import axios from 'axios'

const request = axios.create({
    baseURL: import.meta.env.VITE_API_URL,
    timeout: 5000,
    withCredentials: true,
    headers: {
        'Content-Type': 'application/json',
        'Cache-Control': 'no-cache',
        'Pragma': 'no-cache'
    }
})
request.interceptors.request.use(config => {
    console.log('请求拦截器', config);
    config.params = {
        ...config.params,
        _t: new Date().getTime()
    }
    const token = localStorage.getItem('token')
    if (token) {
        config.headers.Authorization = `Bearer ${token}`  // 添加 Bearer 前缀
    }
    // config.headers['Cache-Control'] = 'no-cache'
    // config.headers['Pragma'] = 'no-cache'
    return config
}, error => {
    return Promise.reject(error)
})

request.interceptors.response.use(
    response => response.data,
    error => {
        if (error.response) {
            console.error('响应错误:', {
                status: error.response.status,
                data: error.response.data,
                headers: error.response.headers
            });
        } else if (error.request) {
            console.error('请求错误:', error.request);
        } else {
            console.error('Error:', error.message);
        }
        return Promise.reject(error);
    }
);

export default request